/*
 * Copyright 2010 Gal Dolber.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.unnison.demo.client.microevents;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHTML;
import com.google.gwt.user.client.ui.HasText;
import com.google.inject.Inject;

import com.unnison.demo.client.alerter.Alerter;
import com.unnison.demo.client.common.HasInnerText;
import com.unnison.demo.client.main.MainPresenter;
import com.unnison.demo.client.microevents.MicroeventsPresenter.MicroeventsBinder;
import com.unnison.framework.client.GuitPresenter;
import com.unnison.framework.client.binder.Attribute;
import com.unnison.framework.client.binder.GuitBinder;
import com.unnison.framework.client.binder.Provided;
import com.unnison.framework.client.binder.ViewField;
import com.unnison.framework.client.binder.ViewHandler;
import com.unnison.framework.client.binder.contributor.HasAttribute;
import com.unnison.framework.client.binder.contributor.RunAsync;
import com.unnison.framework.client.place.Place;
import com.unnison.framework.client.place.PlaceName;

@RunAsync
@PlaceName("micro")
public class MicroeventsPresenter extends GuitPresenter<MicroeventsBinder> implements Place<Void> {

    public interface MicroeventsBinder extends GuitBinder<MicroeventsPresenter> {
    }

    private final MainPresenter hasContent;
    private final Alerter alerter;

    @ViewField
    HasHTML panel;

    @ViewField
    HasInnerText time;
    
    @Provided
    @ViewField
    HasText render = new Button("Render"); // Don't do this in your code

    @Inject
    public MicroeventsPresenter(MainPresenter hasContent, Alerter alerter) {
        this.hasContent = hasContent;
        this.alerter = alerter;
    }

    @Override
    public void go(Void data) {
        hasContent.setContent(getView(), 4);
    }

    @ViewHandler
    @HasAttribute("info")
    void panel$click(@Attribute String info) {
        alerter.alert(info);
    }

    @ViewHandler
    void render$click() {
        long t = System.currentTimeMillis();
        StringBuilder sb = new StringBuilder();
        boolean clickeable = true;
        for (int n = 0; n < 10000; n++) {
            if (clickeable) {
                sb.append("<div info='");
                sb.append(n);
                sb.append("'> DIV ");
            } else {
                sb.append("<div> NOT CLICKEABLE ");
            }
            sb.append(n);
            sb.append("</div>");
            clickeable = !clickeable;
        }
        panel.setHTML(sb.toString());
        time.setInnerText((System.currentTimeMillis() - t) + "ms");
    }
}
